
/**
 * @Title: VouchersController.java
 * @Package org.car.world.controller
 * @Description: TODO(用一句话描述该文件做什么)
 * @author dell
 * @date 2018年11月17日
 * @version V1.0
 */

package org.car.world.controller;

import org.car.world.common.util.Page;
import org.car.world.core.entily.VouchersTable;
import org.car.world.service.TypeService;
import org.car.world.service.VouchersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: VouchersController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author dell
 * @date 2018年11月17日
 *
 */
@Controller
@RequestMapping("admin")
public class VouchersController {
    @Autowired
    VouchersService vouchersService;

    @Autowired
    TypeService typeService;

    @RequestMapping("tovoucherslist")
    public String toVouchersList(@RequestParam(value = "form", required = false, defaultValue = "1") Integer form,
                                 Model model) {
        Page<VouchersTable> page = new Page<VouchersTable>();
        page.setCurrentPage(form);
        page.setPageSize(2);
        page.setTotalRow(vouchersService.selectcount());
        page.setPageList(vouchersService.selectLimit(page.getCurrentPage(), page.getPageSize()));
        model.addAttribute("page", page);
        model.addAttribute("type", typeService.selectAllidentify("TYPE_VOUCHERS"));
        return "vouchers_list";
    }

    @RequestMapping("toupdatevouchers")
    public String toUpdateVouchers(Model model, Integer vouchersId) {
        VouchersTable voucher = vouchersService.selectByPrimaryKey(vouchersId);
        model.addAttribute("vouchers", voucher);
        model.addAttribute("type", typeService.selectAllidentify("TYPE_VOUCHERS"));
        model.addAttribute("type2", typeService.selectAllidentify("TYPE_VOUCHERSSTATE"));
        return "vouchers_update";
    }

    @RequestMapping("doupdatevouchers")
    public String doUpdateVouchers(VouchersTable voucher) {
        int result = vouchersService.updateByPrimaryKey(voucher);
        if (result > 0) {
            return "redirect:tovoucherslist";
        } else {
            return "redirect:toupdatevouchers";
        }
    }

    @RequestMapping("dodelvouchers")
    @ResponseBody
    public Boolean doDelVouchers(Integer vouchersId) {
        int flag = vouchersService.deleteByPrimaryKey(vouchersId);
        if (flag > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("toaddvouchers")
    public String toAddVouchers(Model model) {
        return "vouchers_add";
    }

    @RequestMapping("addvouchers")
    public String doAddVouchers(VouchersTable vouchers) {
        int flag = vouchersService.insert(vouchers);
        if (flag > 0) {
            return "redirect:tovoucherslist";
        } else {
            return "doaddtask";
        }
    }

}
